package com.store.app.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.Utils;

/**
 * 礼券赠送页面
 */
public class CouponGoSendActivity extends BaseActivity  implements IOnHttpListener {
	private TextView title;
	private NetRequest netRequest;
	private EditText et_phone;
	private EditText et_coupon;
	private TextView submit;
	private volatile ProgressDialog mProgressDialog;
	private int tempCoupon = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ExitApplication.getInstance().addActivity(this);
		setContentView(R.layout.fragment_go_send);
		initView();
	}
	private void initView() {
		netRequest = new NetRequest(this);
		netRequest.requestVerifyUserToken(2);
		et_phone = (EditText) findViewById(R.id.et_phone);
		et_coupon = (EditText) findViewById(R.id.et_coupon);
		submit = (TextView) findViewById(R.id.submit);
		submit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				submit();
			}
		});
		title=(TextView) findViewById(R.id.tvTitle);
		title.setText("赠送礼券");
		findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	private void submit() {
		String phone = et_phone.getText().toString().trim();
		if (TextUtils.isEmpty(phone)) {
			Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
			return;
		}
		if(!Utils.matchPhone(phone)){
			Toast.makeText(this, "手机号格式不对!", Toast.LENGTH_SHORT).show();
			return;
		}
		String coupon = et_coupon.getText().toString().trim();
		if (TextUtils.isEmpty(coupon)|coupon.startsWith("0")) {
			Toast.makeText(this, "赠送礼券不能为空", Toast.LENGTH_SHORT).show();
			return;
		}

//		if(Stores.voucher_balance>=0){
			try{
				tempCoupon=Integer.parseInt(coupon);
//				if(Stores.voucher_balance<tempCoupon){
//					Toast.makeText(this, "您的礼券不足！", Toast.LENGTH_SHORT).show();
//					return;
//				}
			}
           catch (Exception e){
				Toast.makeText(this, "请输入整数！", Toast.LENGTH_SHORT).show();
				return;
			}
			if(tempCoupon>200){
				Toast.makeText(this, "礼券单次赠送不能超过200！", Toast.LENGTH_SHORT).show();
				return;
			}
			if(tempCoupon<10){
				Toast.makeText(this, "礼券单次赠送不能少于10！", Toast.LENGTH_SHORT).show();
				return;
			}
//
//		}else{
//			Toast.makeText(this, "您的礼券不足！", Toast.LENGTH_SHORT).show();
//			return;
//		}
		showProgressDialog("", "正在赠送礼券...");

		netRequest.requestSendCoupon(1, phone, coupon);
	}
	@Override
	public void finish() {
		super.finish();
		try {
			((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(getCurrentFocus()
									.getWindowToken(),
							InputMethodManager.HIDE_NOT_ALWAYS);
		}catch (Exception e){}
	}

	@Override
	public void onAfinalFail() {
		dismissProgressDialog();
		Toast.makeText(this,"网络不通,赠送失败",Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onExecuteSuccess(int requestId, String result, String msg) {
		dismissProgressDialog();
		if(requestId==1){
			et_phone.setText("");
			et_coupon.setText("");
			et_phone.setFocusable(true);
			et_phone.setFocusableInTouchMode(true);
			et_phone.requestFocus();
			Toast.makeText(this,"赠送礼券成功",Toast.LENGTH_SHORT).show();
			try {
				Stores.voucher_balance = Stores.voucher_balance - tempCoupon;
			}catch (Exception e){
				e.printStackTrace();
				Log.v("zyl", "礼券运算错误");
			}
			et_coupon.setText("");
		}
	}

	@Override
	public void onExecuteFail(int requestId, String msg) {
		dismissProgressDialog();
		if(requestId==1){
			Toast.makeText(this,"赠送礼券失败:"+msg,Toast.LENGTH_SHORT).show();
		}
	}
	protected void dismissProgressDialog() {
		try {
			if (mProgressDialog != null)
				mProgressDialog.dismiss();
		}catch (Exception e){

		}
	}
	protected void showProgressDialog(String title, String msg) {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(this);
		}
		if(!TextUtils.isEmpty(title)){
			mProgressDialog.setTitle(title);}
		mProgressDialog.setMessage(msg);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setCancelable(false);
		mProgressDialog.show();
	}
}